<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- <script src="js/jquery-3.7.1.min.js"></script> -->
  </head>

  <body>
    <input type="button" id="send" onclick="send()" value="发送" />
    <input type="text" id="message" />
    <script type="text/javascript">
      //WebSocket的四个主要方法open onclose onerror send
      var wsClient = new WebSocket("ws://localhost:4141");
      wsClient.open = function (e) {
        console.log("Connected!");
      };
      wsClient.onclose = function (e) {
        console.log("Disconnected!");
      };
      wsClient.onmessage = function (e) {
        console.log("接收消息:" + e.data);
      };
      wsClient.onerror = function (e) {
        console.log(e.data);
      };

      function send() {
        var oText = document.getElementById("message");
        wsClient.send(oText.value);
      }
    </script>
  </body>
</html>
